﻿using System;

namespace yunio.core
{
    using rehot;
    using rehot.plugin;

    public class YunioUserAgent
    {
        public static string OS { get; internal set; }
        public static string Arch { get; internal set; }
        public static string Version { get; internal set; }
        static YunioUserAgent()
        {
            OS = Read_OS();
            Arch = Read_Arch();
            using (var dll = RehotAssembly.GetDllByType<YunioUserAgent>()) Version = dll.FindAttribute("AssemblyFileVersion").Read<string>("Version");
        }
        public static string Create(string name)
        {
            return $"Mozilla/5.0 ({OS}; {Arch}) {name}WebKit/{Version} (KHTML, like Gecko) {name}/{Version}";
        }
        internal static string Read_OS()
        {
            var os = Environment.OSVersion;
            return os.Platform switch
            {
                PlatformID.Win32NT => "Windows NT " + os.Version,
                PlatformID.Win32Windows => os.Version.Minor == 0 ? "Windows 95" :
                                         os.Version.Minor == 10 ? "Windows 98" :
                                         os.Version.Minor == 90 ? "Windows Me" : "Windows (Win32Windows)",
                PlatformID.Win32S => "Windows 3.1",
                PlatformID.WinCE => "Windows CE",
                PlatformID.Unix => "Unix",
                _ => "Unknown"
            };
        }
        internal static string Read_Arch()
        {
            var arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") ??
                       Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"); // 64 位 WoW64 场景

            if (!string.IsNullOrEmpty(arch))
            {
                if (string.Equals(arch, "AMD64", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(arch, "x86_64", StringComparison.OrdinalIgnoreCase))
                    return "x64";
                if (string.Equals(arch, "x86", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(arch, "x86_32", StringComparison.OrdinalIgnoreCase))
                    return "x86";
                if (string.Equals(arch, "ARM64", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(arch, "aarch64", StringComparison.OrdinalIgnoreCase))
                    return "ARM64";
                if (string.Equals(arch, "ARM", StringComparison.OrdinalIgnoreCase))
                    return "ARM";
                if (string.Equals(arch, "IA64", StringComparison.OrdinalIgnoreCase))
                    return "IA64";
            }

            switch (Environment.OSVersion.Platform)
            {
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                    return IntPtr.Size == 8 ? "x64" : "x86"; // Win32 平台下用指针大小推断
                case PlatformID.Unix:
                    try
                    {
                        var p = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
                        {
                            FileName = "uname",
                            Arguments = "-m",
                            UseShellExecute = false,
                            RedirectStandardOutput = true,
                            CreateNoWindow = true
                        });
                        if (p != null)
                        {
                            var machine = p.StandardOutput.ReadToEnd().Trim();
                            p.WaitForExit();
                            if (!string.IsNullOrEmpty(machine))
                            {
                                if (machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase) ||
                                    machine.StartsWith("aarch", StringComparison.OrdinalIgnoreCase))
                                    return "ARM" + (machine.Contains("64") ? "64" : "");
                                if (machine.Equals("x86_64", StringComparison.OrdinalIgnoreCase))
                                    return "x64";
                                if (machine.Equals("i386", StringComparison.OrdinalIgnoreCase) ||
                                    machine.Equals("i686", StringComparison.OrdinalIgnoreCase))
                                    return "x86";
                                if (machine.Equals("ia64", StringComparison.OrdinalIgnoreCase))
                                    return "IA64";
                            }
                        }
                    }
                    catch { /* 忽略异常，走返回 Unknown */ }
                    return "Unknown";
                default:
                    return "Unknown";
            }
        }
    }
}
